package com.molichuxing.gateway.bff.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.basic.convertor.Convert;
import com.molichuxing.gateway.bff.basic.entity.response.*;
import com.molichuxing.gateway.bff.basic.service.BasicBffService;
import com.molichuxing.services.business.service.RegionBizService;
import com.molichuxing.services.business.service.SubCompanyBizService;
import com.molichuxing.services.infrastructure.dto.response.OwnershipDto;
import com.molichuxing.services.infrastructure.dto.response.RegionCityDto;
import com.molichuxing.services.infrastructure.dto.response.RegionInfoDto;
import com.molichuxing.services.infrastructure.dto.response.SubcompanyDto;
import com.molichuxing.services.infrastructure.service.InsuranceCompanyService;
import com.molichuxing.services.infrastructure.service.OwnershipService;
import com.molichuxing.services.infrastructure.service.RegionService;
import com.molichuxing.services.infrastructure.service.SubcompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("basicBffService")
public class BasicBffServiceImpl implements BasicBffService {
	private static final Logger logger = LoggerFactory.getLogger(BasicBffServiceImpl.class);

	@Resource
	private RegionBizService regionBizService;

	@Resource
	private SubcompanyService subcompanyService;


	@Resource
	private OwnershipService ownershipService;

	@Resource
	private RegionService regionService;

	@Resource
	private SubCompanyBizService subCompanyBizService;

	@Resource
	private InsuranceCompanyService insuranceCompanyService;

	@Override
	public List<InsuranceCompanyVo> getInsurance() {
		return Convert.toInsuranceCompanyVoList(insuranceCompanyService.getInsurance());
	}

	@Override
	public List<RegionCityVo> getAllCity(Integer level) {
		List<RegionCityVo> regionCityVoList =  new ArrayList<RegionCityVo>();

		List<RegionCityDto> regionCityDtoList  =  regionBizService.getAllCity(level);
		if(regionCityDtoList.size()<=0){
			return  regionCityVoList;
		}
		regionCityVoList =  Convert.toRegionCityVoList(regionCityDtoList);
		return  regionCityVoList;
	}

	@Override
	public Paged<SubcompanyVo> getSubcompanyListPage(Map<String, Object> selectParams) {
		logger.info("分子公司列表入参 >>" + JSON.toJSONString(selectParams));
		Paged<SubcompanyVo> subcompanyVoList = new Paged<SubcompanyVo>();
		Paged<SubcompanyDto> dto = subcompanyService.getSubcompanyListPage(ObjectCastUtil.castString(selectParams.get("name")),ObjectCastUtil.castString(selectParams.get("responsibleAccountName")),
						ObjectCastUtil.castInteger(selectParams.get("regionId")),
						ObjectCastUtil.castInteger(selectParams.get("pageNum"))==null ? 1 : ObjectCastUtil.castInteger(selectParams.get("pageNum")),
						ObjectCastUtil.castInteger(selectParams.get("pageSize"))==null ? 10 : ObjectCastUtil.castInteger(selectParams.get("pageSize")));
		List<SubcompanyDto> subcompanyDtoList  =  dto.getList();
		subcompanyVoList = Convert.toPagedSubcompanyVo(dto);
		if(subcompanyVoList.getList() !=null && subcompanyVoList.getList().size()>0){
			for (int i = 0; i < subcompanyVoList.getList().size(); i++) {
				//根据区域id 查询省市
				RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(subcompanyVoList.getList().get(i).getRegionId());
				if(regionInfoDto != null ){
					subcompanyVoList.getList().get(i).setProvinceName(regionInfoDto.getProvinceName()==null ? null :regionInfoDto.getProvinceFullName() );
					subcompanyVoList.getList().get(i).setProvinceId(regionInfoDto.getProvinceId()==null ? null :regionInfoDto.getProvinceId() );
					subcompanyVoList.getList().get(i).setCityName(regionInfoDto.getCityName()==null ? null : regionInfoDto.getCityFullName());
					subcompanyVoList.getList().get(i).setCityId(regionInfoDto.getCityId()==null ? null : regionInfoDto.getCityId());
				}
			}
		}else{
			return  subcompanyVoList;
		}
		logger.info("分子公司列表出参 >>" + JSON.toJSONString(subcompanyVoList));
		return subcompanyVoList;
	}

	@Override
	public SubcompanyVo getSubcompany(Integer id) {
		logger.info("分子公司详情入参 >>" + JSON.toJSONString(id));
		SubcompanyVo subcompanyVo = new SubcompanyVo();
		SubcompanyDto subcompanyDto  = subcompanyService.getSubcompanyNameById(id);
		if(subcompanyDto ==null ){
			return  subcompanyVo;
		}
		RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(subcompanyDto.getRegionId());
		subcompanyVo= Convert.toSubcompanyVo(subcompanyDto);
		subcompanyVo.setProvinceName(regionInfoDto.getProvinceName());
		subcompanyVo.setCityName(regionInfoDto.getCityName());
		return  subcompanyVo;
	}

	@Override
	public Boolean createSubcompany(SubcompanyVo subcompanyVo) {
		int num  =   subcompanyService.createSubcompany(Convert.toSubcompanyCreateDto(subcompanyVo));
		if (num>0){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean modifySubcompany(SubcompanyVo subcompanyVo) {
		int num  =   subcompanyService.modifySubcompany(Convert.toSubcompanyModifyDto(subcompanyVo));
		if (num>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Paged<OwnershipVo> getOwnershipListPage(Map<String, Object> selectParams) {
		logger.info("产权方列表入参 >>" + JSON.toJSONString(selectParams));
		Paged<OwnershipVo> ownershipVoList = new Paged<OwnershipVo>();
		OwnershipVo ownershipVo;
		Paged<OwnershipDto> dto = ownershipService.getOwnershipListPage(ObjectCastUtil.castInteger(selectParams.get("pageSize"))==null ? 10 : ObjectCastUtil.castInteger(selectParams.get("pageSize")),
						ObjectCastUtil.castInteger(selectParams.get("pageNum"))==null ? 1 : ObjectCastUtil.castInteger(selectParams.get("pageNum")));
		List<OwnershipDto> ownershipDtoList  =  dto.getList();
		ownershipVoList = Convert.toPagedOwnershipVo(dto);
		if(ownershipVoList.getList()!=null && ownershipVoList.getList().size()>0){
			for (int i = 0; i < ownershipVoList.getList().size(); i++) {
				//根据区域id 查询省市
				RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(ownershipVoList.getList().get(i).getRegionId());
				if(regionInfoDto != null ){
					ownershipVoList.getList().get(i).setProvinceName(regionInfoDto.getProvinceName()==null ? null :regionInfoDto.getProvinceFullName());
					ownershipVoList.getList().get(i).setProvinceId(regionInfoDto.getProvinceId()==null ? null :regionInfoDto.getProvinceId());
					ownershipVoList.getList().get(i).setCityName(regionInfoDto.getCityName()==null ? null : regionInfoDto.getCityFullName());
					ownershipVoList.getList().get(i).setCityId(regionInfoDto.getCityId()==null ? null : regionInfoDto.getCityId());
				}
			}
		}else{
			return  ownershipVoList;
		}
		logger.info("产权方列表出参 >>" + JSON.toJSONString(ownershipVoList));
		return ownershipVoList;
	}

	@Override
	public Boolean createOwnership(OwnershipVo ownershipVo) {
		int num  =   ownershipService.createOwnership(Convert.toOwnershipCreateDto(ownershipVo));
		if (num>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean modifyOwnership(OwnershipVo ownershipVo) {
		int num  =   ownershipService.modifyOwnership(Convert.toOwnershipModifyDto(ownershipVo));
		if (num>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public OwnershipDetailVo getOwnershipById(Integer ownershipId) {
		OwnershipDetailVo ownershipDetailVo = new OwnershipDetailVo();
		OwnershipDto ownershipDto  = ownershipService.getOwnershipById(ownershipId);
		if(null == ownershipDto) {
			return null;
		}
		BeanUtils.copyProperties(ownershipDto, ownershipDetailVo);
		RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(ownershipDto.getRegionId());
		if(regionInfoDto != null ){
			ownershipDetailVo.setProvinceId(regionInfoDto.getProvinceId());
			ownershipDetailVo.setCityId(regionInfoDto.getCityId());
		}
		return ownershipDetailVo;
	}

	@Override
	public List<SubcompanyVo> getOwnershipByProvinceId(Integer provinceId) {
		return Convert.toSubcompanyVoVoList(subCompanyBizService.getSubcompanyByCityId(provinceId));
	}
}
